package com.wangtianxiang.sxp.weixin.web;

import com.baidu.unbiz.fluentvalidator.Result;

import com.wangtianxiang.sxp.common.dto.PageData;
import com.wangtianxiang.sxp.common.utils.FluentValidateUtils;
import com.wangtianxiang.sxp.dto.ActivityPageDto;
import com.wangtianxiang.sxp.po.Activity;
import com.wangtianxiang.sxp.po.WxUser;
import com.wangtianxiang.sxp.module.service.ActivityService;
import com.wangtianxiang.sxp.weixin.utils.LoginUtils;
import com.wangtianxiang.sxp.common.http.FeedBackBody;
import com.wangtianxiang.sxp.common.http.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wangtianxiang
 * @description 用户控制类
 * @date 2021/4/17
 */
@Slf4j
@Api(tags = "活动管理")
@RequestMapping("/api/activity")
@RestController
public class ActivityController {


    @Autowired
    private ActivityService activityService;

    @Autowired
    private LoginUtils loginUtils;


    @GetMapping(value = "pageList")
    @ApiOperation(value = "获取活动列表")
    public FeedBackBody<PageData<Activity>> pageList(ActivityPageDto dto) throws Exception{
        PageData<Activity> pageData = null;
        Result result = FluentValidateUtils.toSimpleValidate(dto);
        if (!result.isSuccess()){
            return ResponseEntity.setForParamCheckError(pageData).setDetail(result.getErrors().get(0));
        }

        return ResponseEntity.setForSuccess(activityService.pageList(dto));


    }

    @ApiOperation(value = "发布活动")
    @PostMapping(value = "add")
    public FeedBackBody<String> add(@RequestBody Activity activity) throws Exception{
        Result result = FluentValidateUtils.toSimpleValidate(activity);
        if (!result.isSuccess()){
            return ResponseEntity.setForParamCheckError((String)null).setDetail(result.getErrors().get(0));
        }
        WxUser user = loginUtils.getUser();
        activity.setActivityUserId(user.getOpenid());
        activity.setActivityUserName(user.getNickname());
        activityService.saveActivity(activity);
        return ResponseEntity.setForSuccess();
    }
}
